// initial state
// @ts-ignore
import { getUserToken,loginByToken } from "@/service/user/index";
// @ts-ignore
import { message } from "ant-design-vue";

const state = () => ({
  isLogin:false,
  token:'',
  userInfo: {
  },
});
// getters
const getters = {
  // @ts-ignore
  userInfo: (state, getters, rootState) => {
    const userInfoPermission =
      window.localStorage.getItem("userInfoPermission");
    const userInfo = userInfoPermission ? JSON.parse(userInfoPermission) : {};
    return state.userInfo || userInfo;
  },
};

// actions
// @ts-ignore
const actions = {
  // @ts-ignore
  setUserInfoAction({ commit, state }, userInfo) {
    commit("setUserInfo", userInfo);
  },
  // @ts-ignore
  setUserInfoTokenAction({ commit, state }, userInfo) {
    commit("setUserInfo", userInfo);
  },
  // @ts-ignore
  getUserInfoAction({ state }) {
    const userInfoPermission =
      window.localStorage.getItem("userInfoPermission");
    const userInfo = userInfoPermission ? JSON.parse(userInfoPermission) : {};
    return state.userInfo || userInfo;
  },
  // @ts-ignore
  async loginUser({ commit, state }, params):Promise<void>  {
    return new Promise(async (resolve: any, reject: any) => {
      try {
        console.log('params________',params)
        const res:any = await loginByToken(params);
        console.log("res ____________", res);
        const data=res?.data
        console.log('data',data)
        if (data?.code === 200) {
          const userInfo = data.data;
          commit("setUserInfo", userInfo);
          commit("setIsLogin", true);
          window.localStorage.setItem(
              "userInfoPermission",
              JSON.stringify(userInfo)
          );
          message.success(data?.msg);
        } else {
          message.warning(data?.msg);
        }
        resolve({ data});
      } catch (r: any) {
        console.log('r',r)
        message.error(JSON.stringify(r));
        reject(r);
      }
    });
  },
  // @ts-ignore
  async getUserTokenAction({ commit, state }, params):Promise<void>  {
    return new Promise(async (resolve: any, reject: any) => {
      try {
        console.log('params________',params)
        commit("setIsLogin", false);
        const res:any = await getUserToken(params);
        console.log("res ____________", res);
        const data=res?.data
        console.log('data get token',data)
        if (data?.code === 200) {
          commit("setUserToken", data.token);
        } else {
          message.warning(data?.msg);
        }
        resolve({ data});
      } catch (r: any) {
        console.log('r',r)
        message.error(JSON.stringify(r));
        reject(r);
      }
    });
  },
};

// mutations
const mutations = {
  // @ts-ignore
  setUserInfo(state, userInfo) {
    console.log("set info", userInfo);
    window.localStorage.setItem("userInfoPermission", JSON.stringify(userInfo));
    state.userInfo = userInfo;
  },
  // @ts-ignore
  setIsLogin(state, isLogin) {
    state.isLogin = isLogin;
  },
  // @ts-ignore  token 配置
  setUserToken(state, token) {
    console.log("set info token", token);
    window.localStorage.setItem("userInfoPermissionToken", token);
    state.token = token
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations,
};
